Unit testing patterns with Vitest in LivestockAI
Installation
$npx agent-skills-cli install @captjay98/Vitest Patterns
Claude Code
Cursor
Copilot
Codex
Antigravity
Details
Repositorycaptjay98/livestockai
Path.kiro/skills/vitest-patterns/SKILL.md
Branchmain
Scoped Name@captjay98/Vitest Patterns
Usage
After installing, this skill will be available to your AI coding assistant.
Verify installation:
npx agent-skills-cli listSkill Instructions
name: Vitest Patterns description: Unit testing patterns with Vitest in LivestockAI
Vitest Patterns
LivestockAI uses Vitest for fast unit testing with TypeScript support.
Running Tests
# Run unit tests (IMPORTANT: use "bun run test" not "bun test")
bun run test
# Run specific file
bun run test tests/features/batches/batches.property.test.ts
# Run with coverage
bun run test:coverage
# Run integration tests
bun run test:integration
# Run all tests
bun run test:all
Note: bun run test uses Vitest (respects config), while bun test uses Bun's built-in runner (ignores Vitest config).
Test File Organization
tests/
βββ features/ # Feature tests
β βββ batches/
β β βββ batches.property.test.ts
β β βββ batches.test.ts
β βββ sales/
βββ integration/ # Database tests
β βββ batches.integration.test.ts
βββ components/ # Component tests
βββ helpers/ # Test utilities
βββ db-integration.ts
βββ db-mock.ts
Test Naming
- Unit tests:
feature.test.ts - Property tests:
feature.property.test.ts - Integration tests:
feature.integration.test.ts
Basic Test Structure
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
describe('calculateFCR', () => {
it('returns correct FCR for valid inputs', () => {
expect(calculateFCR(150, 100)).toBe(1.5)
})
it('returns null for zero weight gain', () => {
expect(calculateFCR(150, 0)).toBeNull()
})
it('returns null for negative inputs', () => {
expect(calculateFCR(-150, 100)).toBeNull()
})
})
Mocking
import { vi } from 'vitest'
// Mock a module
vi.mock('~/lib/db', () => ({
getDb: vi.fn().mockResolvedValue(mockDb),
}))
// Mock a function
const mockFn = vi.fn().mockReturnValue('result')
// Spy on a function
const spy = vi.spyOn(module, 'function')
Testing Service Layer
Service functions are pure and easy to test:
import { calculateBatchTotalCost, validateBatchData } from './service'
describe('calculateBatchTotalCost', () => {
it('multiplies quantity by cost', () => {
expect(calculateBatchTotalCost(100, 5.5)).toBe('550.00')
})
it('returns zero for invalid inputs', () => {
expect(calculateBatchTotalCost(0, 5.5)).toBe('0.00')
expect(calculateBatchTotalCost(100, -5)).toBe('0.00')
})
})
Coverage Requirements
- Minimum 80% for business logic
- 100% for financial calculations
- Critical path testing for offline functionality
Related Skills
property-testing- Property-based testsintegration-testing- Database teststhree-layer-architecture- Service layer testing
More by captjay98
View allObservability Patterns
0Debugging and monitoring patterns for the distributed offline-first architecture
TanStack Router
0File-based routing, loaders, and navigation patterns in LivestockAI
Agentic Feature Design
0Designing features for the "Action Era" that are AI-accessible by default
TanStack Start
0Server-side rendering and server functions with TanStack Start in LivestockAI
